%% 读取所有翼型文档名,统计数量 
file_path =  '.\Low Reynolds Airfoil\';% 文件夹路径
dat_path_list = dir(strcat(file_path,'*.dat'));%获取该文件夹中所有 dat格式的图像
dat_num = length(dat_path_list);%获取翼型总数量
Alpha = 0:0.5:12;


%% %e实数(科学记数) %f实数(小数) %g由系统自动选取上述两种格式之一 %d整数 %s字符串
%% XFOIL迭代
if dat_num > 0 %有满足条件的图像
    Re = zeros(1,18);
    fidout = fopen('Lift-to-Drag-Ratio.txt','wt'); %打开数据保存路径,所有数据保存在同一个文档
    for f = 2:2 % 迭代雷诺数,放在最外层,从10W到95W一共18个雷诺数
        Re(f-1) = f*5e4; %设置当前雷诺数
        fprintf(fidout,'Re=%d         %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f \n',...
            Re(f-1),Alpha(1),Alpha(2),Alpha(3),Alpha(4),Alpha(5),Alpha(6),Alpha(7),Alpha(8),Alpha(9),Alpha(10),Alpha(11),Alpha(12),Alpha(13),...
            Alpha(14),Alpha(15),Alpha(16),Alpha(17),Alpha(18),Alpha(19),Alpha(20),Alpha(21),Alpha(22),Alpha(23),Alpha(24),Alpha(25));
        for i = 1:dat_num %根据翼型数量dat_num逐一读取图像
            CL = zeros(1,25);
            CD = zeros(1,25);
            dat_name = dat_path_list(i) .name;% 翼型名
            file.name{i,1} = dat_name; %翼型名称写入Struct
            fid = fopen('input.dat','w');
            fprintf(fid,'LOAD \n');
            fprintf(fid, '%s \n',strcat('C:\Users\lenovo\Desktop\xfoil m files\UIUC\',dat_name));
            fprintf(fid,'PANE \n');
            fprintf(fid,'OPER \n');
            fprintf(fid,'VISC %12d \n',Re(f-1)); %定义雷诺数
            fprintf(fid,'M %12.3f \n',0.1);
            fprintf(fid,'ITER %12d \n',100);
            fprintf(fid,'PACC \n');
            fprintf(fid,'output.dat \n');
            fprintf(fid,'\n');
            fprintf(fid,'ASEQ %12.3f %12.3f %12.3f \n',[0 12 0.5]);
            fprintf(fid,'PACC \n'); 
            %             fprintf(fid,'CPWR \n');
            %             fprintf(fid,'PACC \n');
            fprintf(fid,'\n');
            fprintf(fid,'QUIT \n');
            fclose(fid);
            %%
            !xfoil<input.dat &
            pause(5) % where n is a certain number of seconds that I know is too long for the program to still be running
            !taskkill -f -im cmd.exe
            %%
            fid1 = fopen('output.dat','r+');
            line = 0;
            K = zeros(1,25);
            while ~feof(fid1)
                tline1 = fgetl(fid1);
                line = line + 1;
                newline1{line,1} = tline1;
                if line > 12
                    for j = 1:25
                        if str2double(tline1(4:8)) == Alpha(j)
                            CL(j) = str2double(tline1(12:18));
                            CD(j) = str2double(tline1(21:28));
                            K(j) = CL(j)/CD(j);
                        end
                    end
                end
            end
            fclose(fid1);
            delete('output.dat');
            fprintf(fidout,'%s     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f     %f \n',...
                dat_name,K(1),K(2),K(3),K(4),K(5),K(6),K(7),K(8),K(9),K(10),K(11),K(12),K(13),K(14),K(15),...
                K(16),K(17),K(18),K(19),K(20),K(21),K(22),K(23),K(24),K(25));
        end
        fprintf(fidout,'\n');
    end
    fclose(fidout)
end

